#include "threedimensioncrosssectionfind.h"
#include "qsqlrecord.h"
#include "ui_threedimensioncrosssectionfind.h"
#include "util.h"

#include "datacomparison.h"
#include "sqltool.h"

ThreeDimensionCrossSectionFind::ThreeDimensionCrossSectionFind(QWidget *parent)
    : IFindWidget(parent)
    , ui(new Ui::ThreeDimensionCrossSectionFind)
{
    ui->setupUi(this);
    initialize();//init
    reInitUi();
}

ThreeDimensionCrossSectionFind::~ThreeDimensionCrossSectionFind()
{
    delete ui;
    // delete model_find;
}

//ui初始化
void ThreeDimensionCrossSectionFind::initialize()
{
    QStringList nameQstrList;
    nameQstrList.append("");
    nameQstrList.append("equal");
    nameQstrList.append("not equal");
    nameQstrList.append("contain");
    nameQstrList.append("exclude");
    ui->comboBox_name_condition->addItems(nameQstrList);
    ui->comboBox_boundaryType_condition->addItems(nameQstrList);
    ui->comboBox_sequence_condition->addItems(nameQstrList);
    ui->comboBox_sequeceOrder_condition->addItems(nameQstrList);
    ui->comboBox_manufacturingProcess_condition->addItems(nameQstrList);

    QStringList surfaceNetExtendeParentMaterialQstrList;
    surfaceNetExtendeParentMaterialQstrList.append("");
    surfaceNetExtendeParentMaterialQstrList.append("equal");
    surfaceNetExtendeParentMaterialQstrList.append("not equal");
    surfaceNetExtendeParentMaterialQstrList.append("contain all");
    surfaceNetExtendeParentMaterialQstrList.append("exclude all");
    surfaceNetExtendeParentMaterialQstrList.append("contain any");
    surfaceNetExtendeParentMaterialQstrList.append("exclude all");
    ui->comboBox_layerSurface_condition->addItems(surfaceNetExtendeParentMaterialQstrList);
    ui->comboBox_netBoundary_condition->addItems(surfaceNetExtendeParentMaterialQstrList);
    ui->comboBox_extendedBoundary_condition->addItems(surfaceNetExtendeParentMaterialQstrList);
    ui->comboBox_parent_condition->addItems(surfaceNetExtendeParentMaterialQstrList);
    ui->comboBox_material_condition->addItems(surfaceNetExtendeParentMaterialQstrList);

    QStringList stepQstrList;
    stepQstrList.append("");
    stepQstrList.append("equal");
    stepQstrList.append("not equal");
    stepQstrList.append(">");
    stepQstrList.append("<");
    stepQstrList.append(">=");
    stepQstrList.append("<=");
    ui->comboBox_step_condition->addItems(stepQstrList);
}

//查找内容初始化
// void ThreeDimensionCrossSectionFind::updateContext(QSqlQueryModel *model_all)
// {
//     model_find=model_all;
//     //清空combox
//     ui->comboBox_name->clear();
//     ui->comboBox_parent->clear();
//     ui->comboBox_sequece->clear();
//     ui->comboBox_step->clear();
//     ui->comboBox_dropoffOrderIncrement->clear();
//     //添加空白
//     ui->comboBox_name->addItem("");
//     ui->comboBox_parent->addItem("");
//     ui->comboBox_sequece->addItem("");
//     ui->comboBox_step->addItem("");
//     ui->comboBox_dropoffOrderIncrement->addItem("");

//     // qDebug()<<"MainWindow updateContext"<<model_all->rowCount();
//     //依次向查找内容combox中添加现有内容
//     for(int i=0;i<model_all->rowCount();i++)
//     {
//         QSqlRecord  curRec=model_all->record(i);
//         ui->comboBox_name->addItem(curRec.value("Name").toString());
//         ui->comboBox_parent->addItem(curRec.value("Parent").toString());
//         ui->comboBox_sequece->addItem(curRec.value("Sequece").toString());
//         ui->comboBox_step->addItem(curRec.value("Step").toString());
//         ui->comboBox_dropoffOrderIncrement->addItem(curRec.value("DropOffIncrement").toString());
//     }
// }

//初始化打开查找界面


QList<int> ThreeDimensionCrossSectionFind::selectedIds() const
{
    //依次向查找内容combox中添加现有内容


    QVector<ConditionData> conditionDatas;

    ConditionData data;
    data.tableKey = "Name";
    data.condition = ui->comboBox_name_condition->currentText();
    data.b = ui->comboBox_name->currentText();
    conditionDatas.append(data);


    data.tableKey = "Sequence";
    data.condition = ui->comboBox_sequence_condition->currentText();
    data.b = ui->comboBox_sequece->currentText();
    conditionDatas.append(data);

    data.tableKey = "Step";
    data.condition = ui->comboBox_step_condition->currentText();
    data.b = ui->comboBox_step->currentText();
    conditionDatas.append(data);

    data.tableKey = "SequenceOrder";
    data.condition = ui->comboBox_sequeceOrder_condition->currentText();
    data.b = ui->comboBox_sequeceOrder->currentText();
    conditionDatas.append(data);

    data.tableKey = "GlobalStaggerProfile";
    data.condition = ui->comboBox_defaultStaggerProfile_condition->currentText();
    data.b = ui->comboBox_defaultStaggerProfile->currentText();
    conditionDatas.append(data);


    data.tableKey = "DropOffIncrement";
    data.condition = ui->comboBox_dropoffOrderIncrement_condition->currentText();
    data.b = ui->comboBox_dropoffOrderIncrement->currentText();
    conditionDatas.append(data);

    data.tableKey = "ManufacturingProcess";
    data.condition = ui->comboBox_manufacturingProcess_condition->currentText();
    data.b = ui->comboBox_manufacturingProcess->currentText();
    conditionDatas.append(data);

    Sqltool::instance().changeInfoDb();
    auto model_all = Sqltool::instance().selectAllByTableName(lamUtil::tableName);

    return DataComparison::instance().findByModel(model_all, conditionDatas, "id");
}

void ThreeDimensionCrossSectionFind::reInitUi()
{
    Sqltool::instance().changeInfoDb();
    auto model_all = Sqltool::instance().selectAllByTableName(lamUtil::tableName);


    //清空combox
    ui->comboBox_name->clear();
    ui->comboBox_parent->clear();
    ui->comboBox_sequece->clear();
    ui->comboBox_step->clear();
    ui->comboBox_dropoffOrderIncrement->clear();
    //添加空白
    ui->comboBox_name->addItem("");
    ui->comboBox_parent->addItem("");
    ui->comboBox_sequece->addItem("");
    ui->comboBox_step->addItem("");
    ui->comboBox_dropoffOrderIncrement->addItem("");

    // qDebug()<<"MainWindow updateContext"<<model_all->rowCount();
    //依次向查找内容combox中添加现有内容
    for(int i=0;i<model_all.size();i++)
    {
        auto curRec=model_all.at(i);
        ui->comboBox_name->addItem(curRec.value("Name").toString());
        ui->comboBox_parent->addItem(curRec.value("Parent").toString());
        ui->comboBox_sequece->addItem(curRec.value("Sequece").toString());
        ui->comboBox_step->addItem(curRec.value("Step").toString());
        ui->comboBox_dropoffOrderIncrement->addItem(curRec.value("DropOffIncrement").toString());
    }


}


